//
//  SSFileCacheManager.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/10/9.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSFileCacheManager.h"
#import "NSFileManager+Paths.h"

@implementation SSFileCacheManager

+(BOOL)saveObject:(id)object byFileName:(NSString *)fileName {
    NSString *path = [self appendFilePath:fileName];
    path = [path stringByAppendingString:@".archive"];
    BOOL success = [NSKeyedArchiver archiveRootObject:object toFile:path];
    return success;

}

+ (id)getObjectByFileName:(NSString *)fileName {
    NSString *path = [self appendFilePath:fileName];
    path = [path stringByAppendingString:@".archive"];
    id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return obj;
}

+ (void)removeObjectByFileName:(NSString *)fileName {
    NSString *path = [self appendFilePath:fileName];
    path = [path stringByAppendingString:@".archive"];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];

}

+ (NSString *)appendFilePath:(NSString *)fileName {
    NSString *cachesPath = [NSFileManager cachesPath];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",cachesPath,fileName];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:nil]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
    }
    return filePath;

}

#pragma mark - User Default
// 存储用户偏好设置
+ (void)saveUserData:(id)data forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// 读取用户偏好设置
+ (id)readUserDataForKey:(NSString *)key {
    id obj = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return obj;
    
}

// 删除用户偏好设置
+ (void)removeUserDataForkey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


@end
